package com.csust.travel.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.csust.travel.entity.ProductInfo;
import com.csust.travel.entity.ProductVoucher;
import com.csust.travel.model.DTO.ProductInfoDto;
import com.csust.travel.model.DTO.ProductInfoPageDto;
import com.csust.travel.model.R;
import com.csust.travel.model.VO.ProductInfoDetailVo;
import com.csust.travel.model.VO.ProductInfoPageVo;
import com.csust.travel.service.ProductInfoService;
import com.csust.travel.service.ProductVoucherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/product/info")
//@SaCheckLogin
public class ProductInfoController {

    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductVoucherService productVoucherService;

    @PostMapping
    public R CreateProductInfo(@RequestBody ProductInfoDto dto){
        return productInfoService.createProductInfo(dto);
    }


    @GetMapping("/page")
    public R<List<ProductInfoPageVo>> getProductList(ProductInfoPageDto pageDto){
        List<ProductInfoPageVo> result =  productInfoService.getProductList(pageDto);
        return R.ok(result);
    }

    @GetMapping("/my")
    public R<List<ProductInfo>> getMyProductInfo() {
        List<ProductInfo> list = productInfoService.getMyProductInfoList();
        return R.ok(list);
    }


    @GetMapping("/my/collect")
    public R<List<ProductInfo>> getMyProductCollect(){
        List<ProductInfo> list = productInfoService.getMyProductCollectInfo();
        return R.ok(list);
    }


    @GetMapping("/detail")
    public R<ProductInfoDetailVo> getProductInfo(String id){
        ProductInfoDetailVo productInfoDetailVo =productInfoService.getProductInfo(id);
        ProductVoucher productVoucher = productVoucherService.lambdaQuery()
                .eq(ProductVoucher::getProductId,productInfoDetailVo.getId()).one();
        productInfoDetailVo.setProductVoucher(productVoucher);
        return R.ok(productInfoDetailVo);
    }

}
